<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: PageScan.php 425 2011-01-05 10:30:30Z steen.rabol $
 *
 */

Prado::using('Application.Common.Data.PortalToDoRecord');
Prado::using('Application.Common.Data.PortalUserRecord');

class PageScan extends PortalAdminPage
{
	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->setAdminPageTitle('Home');

		if(!$this->IsPostBack)
		{
			$this->dataBind();
		}
	}

	public function dataBind()
	{
		$cdb		= $this->Application->DbConnection;
		$errors		= array();
		$newpages	= array();
		$res		= Null;

		// Scan /Pages for pages to add
		$pagesdir   = str_replace("\\", "/",$this->Application->BasePath . "/Pages");
		$files = rscandir($pagesdir. '/');
		foreach($files as $file)
		{
			$path_parts = pathinfo($file);
			if(isset($path_parts['extension']) && $path_parts['extension'] == "php")
			{
				$dirname	= str_replace($pagesdir,"",$path_parts['dirname']);
				$fullname	= trim(str_replace("\\","/",trim($dirname)));

				if(substr($fullname,0,1) == '/')
				{
					$fullname	= substr($fullname,1);
				}

				$fullname	= str_replace("/",".",trim($fullname));

				if(substr($fullname,0,1) == ".")
				{
					$fullname = substr($fullname,1);
				}

				if($fullname === '/' || $fullname == ".")
				{
					$fullname = "";
				}

				if(strlen($fullname))
				{
					$fullname .= "." . $path_parts['filename'];
				}
				else
				{
					$fullname = $path_parts['filename'];
				}
				$id = $cdb->createCommand("select id from tblpages where fullpath='$fullname'")->queryScalar();

				if($id === false)
				{
					
					try
					{
						$pagename	= $path_parts['filename'];
						$sql		= "insert into tblpages (name, active, created, createdby,fullpath) values ('$pagename', '1', '" . time() . "', '1','$fullname')";
						$cdb->createCommand($sql)->execute();
						$newpages[]['name'] = $pagename;
					}
					catch(TDbException $e)
					{
						$errors[]['name'] = sprintf("%d %s", $e->getCode(), $e->getMessage());
					}
				}
			}
		}

		if(count($newpages) > 0)
		{
			$this->Repeater->Visible	= true;
			$this->Repeater->Enabled	= true;
			$this->Repeater->DataSource		= $newpages;
			$this->Repeater->dataBind();
		}
		else
		{
			$this->Repeater->Visible	= false;
			$this->Repeater->Enabled	= false;
			$this->pnlNoNewPages->Visible	= true;
			$this->pnlNoNewPages->Enabled	= true;
		}

		if(count($errors) != 0)
		{
			$this->ErrRepeater->Visible = true;
			$this->ErrRepeater->Enabled = true;
			$this->ErrRepeater->DataSource = $errors;
			$this->ErrRepeater->dataBind();
		}
	}

	public function GetCssStyle($item)
	{
		switch($item->priority)
		{
			case 0:	return 'background-color:#FFFF80';break;
			case 1:	return 'background-color:#80FF80';break;
			case 2:	return 'background-color:#FF8080';break;
		}
	}
}

function rscandir($base='', &$data=array())
{
	$array = array_diff(scandir($base), array('.', '..', '.svn','System','Admin','Install')); # remove ' and .. from the array */

	foreach($array as $value) /* loop through the array at the level of the supplied $base */
	{
    	if (is_dir(str_replace("\\", "/", $base.$value)))  /* if this is a directory */
    	{
			$data[] = str_replace("\\", "/",$base.$value).'/'; /* add it to the $data array */
			$data = rscandir($base.$value.'/', $data); /* then make a recursive call with the current $value as the $base supplying the $data array to carry into the recursion */
		}
		elseif (is_file(str_replace("\\","/",$base.$value))) /* else if the current $value is a file */
		{
			$data[] = str_replace("\\", "/",$base.$value); /* just add the current $value to the $data array */
		}
	}

	return $data; // return the $data array
}
?>